package com.yuehuanghun.framework.web.resolver;

import java.time.LocalDateTime;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.yuehuanghun.framework.util.StringUtils;

import cn.hutool.core.date.DateUtil;

/**
 * 请求参数绑定解析器，针对java.time.LocalDateTime
 * 目前仅支持yyyy-MM-dd HH:mm:ss格式
 * @author yadmin
 *
 */
public class LocalDateTimeResolver implements HandlerMethodArgumentResolver {

	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return parameter.getParameterType() == LocalDateTime.class;
	}

	@Override
	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
		String paramValue = webRequest.getParameter(parameter.getParameterName());
		if(StringUtils.isBlank(paramValue)) {
			return null;
		}
		
		return DateUtil.parseLocalDateTime(paramValue);
	}

}
